﻿(function () {
    var paras = {
        isWeek: false,
        days: '',
        minDate: '',
        maxDate: '',
        disabledDates: '',
        dayNum: 0
    };

    var addressOpera = {

        init: function (isWeek, days, minDate, maxDate, disabledDates, dayNum) {
            paras.isWeek = isWeek;
            paras.days = days;
            paras.minDate = minDate;
            paras.maxDate = maxDate;
            paras.disabledDates = disabledDates;
            paras.dayNum = dayNum;

            addressOpera.pickerClickEvent();
            addressOpera.dataFormart();

        },
        pickerClickEvent: function () {
            $('#shipmentDataPickerId').datetimepicker(
               {
                   minDate: paras.minDate,
                   maxDate: paras.maxDate,
                   onGenerate: function (ct) {
                       jQuery(this).find('.xdsoft_date.xdsoft_weekend').select()
                         .addClass('xdsoft_disabled');
                   },
                   onSelectDate: function (dp, $input) {
                       addressOpera.GetTheDayTimeSpanToHtml($input.val(), paras.days, paras.dayNum);
                   },
                   disabledDates: paras.disabledDates,
                   format: 'Y/m/d',
                   timepicker: false
               }
           );
        },

        dataFormart: function () {
            Date.prototype.Format = function (fmt) { //author: meizz
                var o = {
                    "M+": this.getMonth() + 1, //月份
                    "d+": this.getDate(), //日
                    "h+": this.getHours(), //小时
                    "m+": this.getMinutes(), //分
                    "s+": this.getSeconds(), //秒
                    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                    "S": this.getMilliseconds() //毫秒
                };
                if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
                for (var k in o)
                    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
                return fmt;
            }
        },

        GetTheDayTimeSpanToHtml: function (day, days, dayNum) {
            if (days == null || days.length <= 0) {
                $("#shipmentDataPickerTimespans").html('');
                $("#TimespansDiv").hide();
                checkScheduleDate(true);
                return;
            }
            function z(n) { return (n < 10 ? '0' : '') + n; }
            for (var i = 0; i < dayNum; i++) {
                var d = new Date(Date.parse(JSON.parse(days)[i].Time));
                var parsedDate = d.getUTCFullYear() + '/' + z(d.getUTCMonth() + 1) + '/' + z(d.getUTCDate());
                if (day == parsedDate) {
                    var timspans = JSON.parse(days)[i].ShipmentTimeSpans;
                    var timespancount = JSON.parse(days)[i].ShipmentTimeNum;
                    $("#shipmentDataPickerTimespans").html('');
                    for (var k = 0; k < timespancount; k++) {
                        var sid = timspans[k].Id;
                        var stringspan = '' + timspans[k].NoEarlyThan + ' : 00' + '----' + timspans[k].NoLateThan + ' : 00';
                        $('#shipmentDataPickerTimespans').append('<option value="' + sid + '">' + stringspan + '</option>');
                    }
                }
            }
            $("#TimespansDiv").show();
            checkScheduleDate(true);
        }

    };
    window.addressOpera = addressOpera;
})();